module.exports = function(args){
	var _this = this;
    args = args || {};
    this.runing = false;
    this.active = 0;
    this.duration = args.duration || 500;
    this.timer = '';
    this.count = args.count || 0;
    this.show = args.show || '';
    this.n = Math.ceil(args.windowSize / args.boxSize);//单行显示个数
    this.id = 'indicator' + new Date().getTime();
    a.Storage.set(this.id,true);
    this.play = function(x){
    	if(this.runing) return false;
    	if(this.timer) clearTimeout(this.timer);
    	this.runing = true;
    	if(args.hide) args.hide.call(this);
    	this.active += x;
    	this._show(this.active,false,function(){
            if(_this.runing) _this.end();
        });
    }
    this.goPlay = function(active){
        this.play(active - this.active % this.count);
    }
    this.end = function(){
        this.runing = false;
    	if(this.active == 0) {
			this.active = this.count;
            this._show(this.active,true);
		}else if(this.active == this.count * 3 - this.n){
			this.active = this.count * 2 - this.n;
            this._show(this.active,true);
		}
		this.timer = setTimeout(function(){
            if(a.Storage.get(_this.id)){
                _this.timer = '';
                _this.play(args.playCount || 1);
            }
		},args.interval || 3000)
    }
    this._show = function(active,isEnd,next){
        var style = args.style.replace(/{p}/igm,active * args.boxSize).replace(/{d}/igm,!isEnd ? this.duration / 1000 : 0);
        if(typeof this.show == 'function') this.show.call(this,style,-(active * args.boxSize),isEnd);
        if (next) {
          setTimeout(function(){
            next();
          },this.duration)
        }
    }
    this.init = function(active){
        this.runing = false;
        this.active = 0;
    }
}